package com.student.Activitys.http.manager;

import android.app.Activity;
import android.util.Log;

import com.student.Activitys.bean.InfoReturnBean;
import com.student.Activitys.bean.PartyMyApplyBean;
import com.student.Activitys.http.ApiService;
import com.student.Activitys.http.NetWorkMgr;
import com.student.Activitys.http.api.PartyAPI;
import com.student.Activitys.utils.Constant;

import org.simple.eventbus.EventBus;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * 项目名称：
 * 类描述：所有 思政  接口管理 manager
 * 创建人：张飞祥
 * 创建时间：2017/9/27 0027 下午 1:33
 * 修改人：张飞祥
 * 修改时间：2017/9/27 0027 下午 1:33
 * 修改备注：
 */
public class PartyManager {

    private static PartyAPI mPartyAPI;
    private static ApiService mApiService;
    private static Activity mActivity;
    private static int mTaskId;

    //加载数据类型
    private int RefType = Constant.FIRST_LOAD;

    private static class PartyApiManagerHolder {
        private static final PartyManager INSTANCE = new PartyManager();
    }
    public static final PartyManager getPartyApiManager(Activity activity, int taskId) {
        mPartyAPI = NetWorkMgr.getInstance(activity)
                .getServiceAPI(PartyAPI.class);
        mApiService = ApiService.getApiService();
        mActivity = activity;
        mTaskId = taskId;
        return PartyApiManagerHolder.INSTANCE;
    }
    public void getType(int type){
        RefType=type;
    }

    /**
     *
     * 思政：入党申请 数据处理请求
     *
     */
    public void partyApply(String action, String xh,String mobile,String mail, String qq,
                           String weixin,String familyAddr,String sfzh,
                           String fatherName,String fatherTel, String fatherWorkPlace,String fatherJob,
                           String matherName,String matherTel, String matherWorkPlace,String matherJob,
                           String rdzy,String brjl) {
        Subscription subscription = mPartyAPI
                .partyApply(action,xh,mobile,mail,qq,weixin,familyAddr,
                        sfzh,fatherName,fatherTel,fatherWorkPlace,fatherJob,matherName,matherTel,
                        matherWorkPlace,matherJob,rdzy,brjl).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<InfoReturnBean>() {
                    @Override
                    public void onCompleted() {
                        //加载完成,可用于展示进度条
                        EventBus.getDefault().post("onCompleted", Constant.BUS_PARTY_SUBMIT_ON_COMPLETED);
                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e(mActivity.getClass().getSimpleName(),"PartyList(思政：入党申请提交申请) 错误" + e.toString());
                    }
                    @Override
                    public void onNext(InfoReturnBean bean) {
                        //返回数据处理
                        if (bean != null) {
                                EventBus.getDefault().post(bean, Constant.BUS_PARTY_SUBMIT);
                        }else{
                            Log.e(mActivity.getClass().getSimpleName(), "数据源为空");
                        }
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     *
     * 思政：入党申请 数据处理请求
     *
     */
    public void partyMyApplyList(String action, String xh) {
        Subscription subscription = mPartyAPI
                .partyMyApplyList(action,xh).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<PartyMyApplyBean>() {
                    @Override
                    public void onCompleted() {
                        //加载完成,可用于展示进度条
                        EventBus.getDefault().post("onCompleted", Constant.BUS_PARTY_APPLY_MY_ON_COMPLETED);
                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e(mActivity.getClass().getSimpleName(),"partyMyApplyList(思政：我的入党申请 提交申请) 错误" + e.toString());
                    }
                    @Override
                    public void onNext(PartyMyApplyBean bean) {
                        //返回数据处理
                        if (bean != null) {
                                EventBus.getDefault().post(bean, Constant.BUS_PARTY_APPLY_MY);
                        }else{
                            Log.e(mActivity.getClass().getSimpleName(), "数据源为空");
                        }
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

}
